package ajaxjs.net;

import javax.servlet.http.HttpServletRequest;
import ajaxjs.lang.servletHandler;

public class UA {
	private HttpServletRequest request;
	
	@servletHandler
	public UA(HttpServletRequest request){
		this.request = request;
	}
	
	/**
	 * 获取请求的 UA
	 * @param _request
	 * @return
	 */
	public String getUA_String(){
		String ua = request.getHeader("User-Agent");
		
		if (ua == null || "".equals(ua))
			throw new NullPointerException("缺少 request UA 参数！ ");
		
		return ua.toLowerCase(); // 强制转换为小写字母
	}
	
	public boolean isIE(){
		return getUA_String().contains("msie");
	}
	
	public boolean is_old_IE(){
		String ua = getUA_String();
		return isIE() && (ua.contains("msie 5.5") || ua.contains("msie 6.0") || ua.contains("msie 7.0")  || ua.contains("msie 8.0"));
	}
	
	public boolean isFireFox(){
		return getUA_String().contains("firefox");
	}
	public boolean isChrome(){
		return getUA_String().contains("chrome");
	}
	public boolean isAndroid(){
		return getUA_String().contains("android");
	}
	public boolean isIPhone(){
		return getUA_String().contains("iphone");
	}
	public boolean isIPad(){
		return getUA_String().contains("ipad");
	}
	public boolean isIOS(){
		return isIPad() || isIPhone();
	}
	
	public boolean isWinPhone(){
		return getUA_String().contains("windows phone");
	}
	
	/**
	 * 是否为手机
	 * @return
	 */
	public boolean isPhone(){
		return isAndroid() || isIOS() || isWinPhone();
	}
}